//
//  HomeFor523ViewController.m
//  CP20180523
//
//  Created by apple on 2018/5/24.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "HomeFor523ViewController.h"
#import "NearShopController.h"
@interface HomeFor523ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (nonatomic,retain)NSMutableDictionary* dataDic;

@property (weak, nonatomic) IBOutlet UIView *winNumberBoxView;
@property (weak, nonatomic) IBOutlet UILabel *preNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *nextOpenTime;
@property (weak, nonatomic) IBOutlet UILabel *infoLabel;


@end

@implementation HomeFor523ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = [Home523Manager shared].lotteryName;
    self.titleLabel.text = [Home523Manager shared].lotteryName;
    self.dataDic = [NSMutableDictionary dictionaryWithCapacity:0];
    
    [self loadData];
    [self addRightNavBar];
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    self.tabBarController.tabBar.hidden = YES;
}
-(void)loadData{
    [self.dataDic removeAllObjects];
    [[SelectionNumManager shared] getDataWithLotteryID:@"7" andActionID:@"201" andCallBack:^(BOOL load, NSDictionary *resObj) {
        [self.dataDic addEntriesFromDictionary:resObj[@"data"]];
        [self setLotteryDetail];
    }];
    
}

-(void)setLotteryDetail{
//    NSLog(@"self.dataDic = %@",self.dataDic);
    self.preNameLabel.text = self.dataDic[@"PreName"];
    for (UIView* view in self.winNumberBoxView.subviews) {
        if (view.tag == 1001) {
            [view removeFromSuperview];
        }
    }
    NSArray* numberArray = [self.dataDic[@"PreWinNumber"] componentsSeparatedByString:@","];
    if ([numberArray count]>=5) {

        self.infoLabel.hidden = YES;
        CGFloat ballWidth = 36;
        CGFloat spacing = 12;
        CGFloat spacingX = 0;
        for (int i=0; i<[numberArray count]; i++) {
            UIView* numberView = [[UIView alloc] initWithFrame:CGRectMake(i*(ballWidth+spacing), 0, ballWidth, ballWidth)];
            numberView.backgroundColor = [UIColor whiteColor];
            UILabel* numberLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ballWidth, ballWidth)];
            numberLabel.font = [UIFont boldSystemFontOfSize:15];
            numberLabel.textColor = kRGBColor(16,147,255);
            numberLabel.textAlignment = NSTextAlignmentCenter;
            numberLabel.text = [numberArray objectAtIndex:i];
            [numberView addSubview:numberLabel];
            numberView.layer.cornerRadius = ballWidth*0.5;
            numberView.tag = 1001;
            [self.winNumberBoxView addSubview:numberView];
            spacingX+=(ballWidth+spacing);
        }
    }else{
        self.infoLabel.hidden = NO;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(30.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self loadData];
        });
    }
    
    
    NSString* openTimeString = self.dataDic[@"OpenTime"];
    NSString* nowTimeString = self.dataDic[@"NowTime"];
    nowTimeString = [nowTimeString stringByReplacingOccurrencesOfString:@"/" withString:@"-"];
    NSString* dayString = [nowTimeString componentsSeparatedByString:@" "][0];
    
    openTimeString = [openTimeString stringByReplacingOccurrencesOfString:@"开奖" withString:@":00"];
    
    
    
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
    
    NSDate* nowDate = [formatter dateFromString:nowTimeString];
    NSDate* openDate = [formatter dateFromString:[NSString stringWithFormat:@"%@ %@",dayString,openTimeString]];
    
//    NSLog(@"%@,\n %@ \n time = %lf",nowDate,openDate,[openDate timeIntervalSinceDate:nowDate]);
    NSTimeInterval countdowmTime = [openDate timeIntervalSinceDate:nowDate];
    [self CountdownTime:(NSInteger)countdowmTime];
}


#pragma mark - 倒计时
-(void)CountdownTime:(NSInteger)timeCount{
    __block NSInteger second = timeCount;
    dispatch_queue_t quene = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, quene);
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer, ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            if (second == 0) {
                dispatch_cancel(timer);
                [self loadData];
            } else {
                self.nextOpenTime.text = [NSString stringWithFormat:@"距下期开奖：%@",[self getMMSSFromSS:second]];
                second--;
            }
        });
    });
    dispatch_resume(timer);
}

-(NSString *)getMMSSFromSS:(NSInteger)seconds{
    
    NSInteger minute = seconds/60;
    NSInteger second = seconds%60;
    NSString *str_minute = minute<10?[NSString stringWithFormat:@"0%ld",minute]:[NSString stringWithFormat:@"%ld",minute];
    NSString *str_second = second<10?[NSString stringWithFormat:@"0%ld",second]:[NSString stringWithFormat:@"%ld",second];
    NSString *format_time = [NSString stringWithFormat:@"%@:%@",str_minute,str_second];
    
    return format_time;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - 号码分析
- (IBAction)analysisAction:(id)sender {
    UIViewController* acViewCon = [[UIStoryboard storyboardWithName:@"NumberAnalysis" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"NumberAnalysisViewController"];
    [self.navigationController pushViewController:acViewCon animated:YES];
}

#pragma mark - 号码历史
- (IBAction)winNumHistoryAction:(id)sender {
    UIViewController* acViewCon = [[UIStoryboard storyboardWithName:@"WinNumHistory" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"WinNumHistoryViewController"];
    [self.navigationController pushViewController:acViewCon animated:YES];
}

#pragma mark - 走势
- (IBAction)trendAction:(id)sender {
    UIViewController* acViewCon = [[UIStoryboard storyboardWithName:@"TrendForLot" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"TrendViewController"];
    [self.navigationController pushViewController:acViewCon animated:YES];
}

#pragma mark - 预测
- (IBAction)predictionAction:(id)sender {
    UIViewController* acViewCon = [[UIStoryboard storyboardWithName:@"NumberPrediction" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"NumPreViewController"];
    [self.navigationController pushViewController:acViewCon animated:YES];
}

#pragma mark - 开奖
- (IBAction)historyListAction:(id)sender {
//
//
    [LotteryManager sharedManager].currentLotteryID = @"7";
    [LotteryManager sharedManager].currentLotteryName = @"11选5";
    UIViewController* acViewCon = [[UIStoryboard storyboardWithName:@"Announcement" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"AnnounceNumViewController"];
    [self.navigationController pushViewController:acViewCon animated:YES];
}

#pragma mark - 选号
- (IBAction)selectedNumberAction:(id)sender {
    [SelectionNumManager shared].currentLotteryID = @"7";
    [SelectionNumManager shared].currentLotteryName = @"11选5";
    
    UIViewController* acViewCon = [[UIStoryboard storyboardWithName:@"SelectedNumber" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"Select11X5ViewController"];
    [self.navigationController pushViewController:acViewCon animated:YES];
    
}


#pragma mark - 遗漏情况
- (IBAction)missNumberAction:(id)sender {
//
    UIViewController* acViewCon = [[UIStoryboard storyboardWithName:@"MissedNumber" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"MissedNumberViewController"];
    [self.navigationController pushViewController:acViewCon animated:YES];
}

#pragma mark - AC过滤
- (IBAction)ACNumberAction:(id)sender {
    UIViewController* acViewCon = [[UIStoryboard storyboardWithName:@"11X5ACNumber" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:@"ACNumberViewController"];
    [self.navigationController pushViewController:acViewCon animated:YES];
}

#pragma mark - 附近彩店
- (IBAction)goNearTheShopPageClickAction:(UIButton *)sender {
    NearShopController *nearShopVC = [NearShopController new];
    nearShopVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:nearShopVC animated:NO];
    
}
#pragma mark - 你想知道的11选5都在这里
- (IBAction)goAboutPageClickAction:(UITapGestureRecognizer *)sender {
    [self.navigationController pushViewController:[[UIStoryboard storyboardWithName:@"WantKnowStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"WantKnowController"] animated:YES];
    
}
#pragma mark - 论坛
- (IBAction)goLunTanPageClickAction:(UITapGestureRecognizer *)sender {
    
    [self.navigationController pushViewController:[[UIStoryboard storyboardWithName:@"BBSStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"BBSController"] animated:YES];
    
    
}

#pragma mark - 技巧总结

- (IBAction)goSkillsPageClickAction:(UIButton *)sender {

    [self.navigationController pushViewController:[[UIStoryboard storyboardWithName:@"WantKnowStoryboard" bundle:nil] instantiateViewControllerWithIdentifier:@"SkillsSummaryController"] animated:YES];
    
}
- (void)addRightNavBar
{
//    UIButton* _btnRight = [UIButton buttonWithType:UIButtonTypeCustom];
//    [_btnRight setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
////    [_btnRight setTitle:@"发送" forState:UIControlStateNormal];
//    [_btnRight setImage:[UIImage imageNamed:@"个人中心"] forState:UIControlStateNormal];
//    [_btnRight setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [_btnRight setBounds:CGRectMake(0, 0, 40, 44)];
//    UIBarButtonItem* Rightitem  = [[UIBarButtonItem alloc] initWithCustomView:_btnRight];
//    [_btnRight addTarget:self action:@selector(rightButtonAction) forControlEvents:UIControlEventTouchUpInside];
//    self.navigationItem.rightBarButtonItem = Rightitem;
}
- (void)rightButtonAction

{
    if ([OpenLoginManager openLoginPageWithController:self]) {
        return;
    }
    [self.navigationController pushViewController:[[UIStoryboard storyboardWithName:@"Storyboard_5" bundle:nil]instantiateViewControllerWithIdentifier:@"ModuleVC5"] animated:YES];
    
}
@end
